package com.csw.encryptiontechnology.utils.mapperRSA;


import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.bouncycastle.util.encoders.Hex;


public class Sm4Util {


    public static final String key = "0123456789abcdeffedcba9876543210";

    public static void main(String arg[]) throws Exception {

        String paramStr = "pass$123";

        String arfter = encrypt(paramStr);
        String brfore = decrypt(arfter);
        arfter = arfter.toLowerCase();
        System.out.println("明文:---------------------" + paramStr);
        System.out.println("加密后密文:---------------------" + arfter);
        System.out.println("解密后明文:---------------------" + brfore);

    }


    /**
     * 加密
     */

    public static String encrypt(String data) {
        byte[] sm4KeyBytes = Hex.decode(key);
        SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", sm4KeyBytes);
        return sm4.encryptHex(data).toUpperCase();
    }


    /**
     * 解密
     */

    public static String decrypt(String data) {
        try {
            byte[] sm4KeyBytes = Hex.decode(key);
            SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", sm4KeyBytes);
            return sm4.decryptStr(data);
        } catch (Exception e) {
            return data;
        }
    }
}
